#include <QtGui>
#include "loadwindow.h"
#include "mainwindow.h"

CLoadWindow::CLoadWindow()
{
    QHBoxLayout *top = new QHBoxLayout;
    QHBoxLayout *mid = new QHBoxLayout;
    QHBoxLayout *bottom = new QHBoxLayout;
    QLabel *LMain = new QLabel("Select directory:");
    QLabel *CDir = new QLabel("Directory:");

    directoryComboBox = createComboBox(QDir::currentPath());

    BSelDir = new QPushButton ("Browse");
    connect (BSelDir, SIGNAL(clicked()), this, SLOT(browse()));
    files = new QStringList;
    EPath = new QTextEdit;
    QStringList files1;

    mid->addWidget(CDir);
    mid->addWidget(directoryComboBox);
    mid->addWidget(BSelDir);

    top->addWidget(LMain);

    createFilesTable();

    //mid->addWidget(files1);
    QVBoxLayout *mainlayout = new QVBoxLayout;

    mainlayout->addLayout(top);
    mainlayout->addLayout(mid);

    bottom->addWidget(filesTable);

    mainlayout->addLayout(bottom);

    QHBoxLayout *filebtn = new QHBoxLayout;
    patientFile = new QPushButton (tr("File creation"));
    connect(patientFile, SIGNAL(clicked()), this, SLOT(makeFile()));

    filebtn->addWidget(patientFile);

    mainlayout->addLayout(filebtn);
    setLayout(mainlayout);
    setWindowTitle(tr("Select directory"));
}

void CLoadWindow::makeFile()
{
    patient.patientFile();
}

void CLoadWindow::browse()
{
    QString directory = QFileDialog::getExistingDirectory(this,
                                    tr("Find Files"), QDir::currentPath());


         if (!directory.isEmpty()) {
             QString directory2=directory;
             for (int i=0; i< directory2.size(); i++)
             {
                 if (directory2[i]=='/') directory2[i]='\\';
             }

             if (directoryComboBox->findText(directory) == -1)
                 directoryComboBox->addItem(directory);
             directoryComboBox->setCurrentIndex(directoryComboBox->findText(directory));
             patient.load(directory2.toStdString());


             QStringList f;

             for (int i=0; i<patient.files.size(); i++)
             {


                 QString temp1;
                 string temp2;
                 temp2=patient.files[i];
                 temp1 = QString::fromStdString(temp2);
                 f.push_back(temp1);
                 temp1.clear();
             }
             for (int i=0; i<f.size(); i++)
             {
                 for (int j=0; j<f[i].size(); j++)
                     if (f[i][j]=='\\') f[i][j]='/';
             }
             showFiles(f);
         }

}


QComboBox *CLoadWindow::createComboBox(const QString &text)
 {
     QComboBox *comboBox = new QComboBox;
     comboBox->setEditable(true);
     comboBox->addItem(text);
     comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
     return comboBox;
 }

void CLoadWindow::createFilesTable()
 {
     filesTable = new QTableWidget(0, 2);
     filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);

     QStringList labels;
     labels << tr("File Name") << tr("Size");
     filesTable->setHorizontalHeaderLabels(labels);
     filesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
     filesTable->verticalHeader()->hide();
     filesTable->setShowGrid(false);

     connect(filesTable, SIGNAL(cellClicked(int,int)),
             this, SLOT(openFileOfItem(int, int)));
 }

void CLoadWindow::openFileOfItem(int row, int /* column */)
 {
     QString file = filesTable->item(row, 0)->text();
     for (int i=0; i<file.size(); i++)
     {
         if (file[i]=='/') file[i]='\\';
     }
     cellwindow = new CMainWindow(file.toStdString());
     cellwindow->show();


     //QDesktopServices::openUrl(QUrl::fromLocalFile(currentDir.absoluteFilePath(item->text())));
 }


void CLoadWindow::showFiles(const QStringList &files)
 {
     for (int i = 0; i < files.size(); ++i) {
         QFile file(currentDir.absoluteFilePath(files[i]));
         qint64 size = QFileInfo(file).size();

         QTableWidgetItem *fileNameItem = new QTableWidgetItem(files[i]);
         fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
         QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB")
                                              .arg(int((size + 1023) / 1024)));
         sizeItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
         sizeItem->setFlags(sizeItem->flags() ^ Qt::ItemIsEditable);

         int row = filesTable->rowCount();
         filesTable->insertRow(row);
         filesTable->setItem(row, 0, fileNameItem);
         filesTable->setItem(row, 1, sizeItem);
     }
}
